package cn.turing.db;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.turing.model.Student;

public class StudentDao
{
	
	/**
	 * @author walker
	 * 
	 * @return ���� ����ѧ����Ϣ
	 */
	public List<Student> list()
	{
		List<Student> students=new ArrayList<Student>();
		String sql="select * from student";
		
		Connection con=MyDB.getConn();
		ResultSet rs=MyDB.getResultSet(con, sql);
		try
		{
			while(rs.next())
			{
				Student student=new Student();
				student.setId(rs.getInt("id"));
				student.setUsername(rs.getString("username"));
				student.setHp(rs.getString("hp"));
				student.setDamage(rs.getString("damage"));
				student.setBeauty(rs.getString("beauty"));
				
				students.add(student);
			}
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
		MyDB.closeConnection(con);
		return students;
	}
	
	/**
	 * ����ѧ����Ϣ
	 * @param s
	 * @return
	 */
	public int save(Student s)
	{
		int result=0;
		String sql="insert into student(username,hp,damage,beauty)"
				+ " values('"+s.getUsername()+"','"+s.getHp()+"','"+s.getDamage()+"','"+s.getBeauty()+"')";
		
		System.out.println(sql);
		Connection con=MyDB.getConn();
		result=MyDB.executeUpdateSql(con, sql);
		MyDB.closeConnection(con);
		return result;
	}
	/**
	 * �༭ѧ����Ϣ
	 * @param s
	 * @return
	 */
	public int edit(Student s)
	{
		int result=0;
		String sql="update student set username='"+s.getUsername()+
				"',hp='"+s.getHp()+"',damage='"+s.getDamage()+"',beauty='"+s.getBeauty()+"' where id="+s.getId();
		System.out.println(sql);
		Connection con=MyDB.getConn();
		result=MyDB.executeUpdateSql(con, sql);
		MyDB.closeConnection(con);
		return result;
	}
	/**
	 * ɾ��ѧ����Ϣ
	 * @param s
	 * @return
	 */
	public int delete(int id)
	{
		int result=0;
		String sql="delete from student where id="+id;
		Connection con=MyDB.getConn();
		result=MyDB.executeUpdateSql(con, sql);
		MyDB.closeConnection(con);
		return result;
	}
}
